extends Node
class_name SaveHandler

@export var save_name: String
@export var player: CharacterBody3D
@export var items: Array[ItemData]

var pick_upable_scene = preload("res://gameObjects/interactables/pick_upable.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
	load_file()

func clear_file():
	var dir = DirAccess.open("user://")
	var save_dir: String
	if not dir.dir_exists("saves"):
		return
	dir.change_dir("saves")
	if not dir.dir_exists(save_name):
		return
	dir.change_dir(save_name)
	save_dir = dir.get_current_dir()
	if dir.file_exists("player.json"):
		dir.remove("player.json")
	if dir.dir_exists("inventories"):
		for file in DirAccess.get_files_at(save_dir + "/inventories"):
			dir.remove("inventories/" + file)
		if dir.dir_exists("inventories/external"):
			for file in DirAccess.get_files_at("inventories/external"):
				dir.remove("inventories/external/" + file)
		if dir.dir_exists("inventories/dropped"):
			for file in DirAccess.get_files_at(save_dir + "/inventories/dropped"):
				print(dir.remove("inventories/dropped/" + file))

func save_file():
	clear_file()
	var dir = DirAccess.open("user://")

	if not dir.dir_exists("saves"):
		dir.make_dir("saves")
	dir.change_dir("saves")
	if not dir.dir_exists(save_name):
		dir.make_dir(save_name)
	dir.change_dir(save_name)
	if not dir.dir_exists("inventories"):
		dir.make_dir("inventories")
	if not dir.dir_exists("inventories/external"):
		dir.make_dir("inventories/external")
	if not dir.dir_exists("inventories/dropped"):
		dir.make_dir("inventories/dropped")
	var dir_str = dir.get_current_dir()

	var player_data = player.save_dict()
	var player_data_json = JSON.stringify(player_data)
	var file = FileAccess.open(dir_str + "/player.json", FileAccess.WRITE)
	file.store_string(player_data_json)
	file.close()

	var player_inventory_data = player.save_inventory_array()
	var player_inventory_data_json = JSON.stringify(player_inventory_data)
	file = FileAccess.open(dir_str + "/inventories/player.json", FileAccess.WRITE)
	file.store_string(player_inventory_data_json)
	file.close()

	var external_inventory_data
	var external_inventory_data_json
	var external_inventories = get_tree().get_nodes_in_group("external_inventory")
	var inventory_name = "broken"
	for i in external_inventories:
		inventory_name = i.inventory_id
		external_inventory_data = i.save_inventory_array()
		external_inventory_data_json = JSON.stringify(external_inventory_data)
		file = FileAccess.open(dir_str + "/inventories/external/" + inventory_name + ".json", FileAccess.WRITE)
		file.store_string(external_inventory_data_json)
		file.close()

	var dropped_items = get_tree().get_nodes_in_group("pick_upable")
	for i in range(dropped_items.size()):
		file = FileAccess.open(dir_str + "/inventories/dropped/" + str(i) + ".json", FileAccess.WRITE)
		var item = dropped_items[i]
		var item_data
		var item_data_json
		item_data = item.save_slot()
		item_data_json = JSON.stringify(item_data)
		file.store_string(item_data_json)
		file.close()

func load_file():
	var dir = DirAccess.open("user://")

	if not dir.dir_exists("saves"):
		return
	dir.change_dir("saves")
	if not dir.dir_exists(save_name):
		return
	dir.change_dir(save_name)
	if not dir.dir_exists("inventories"):
		return
	if not dir.dir_exists("inventories/external"):
		return
	if not dir.dir_exists("inventories/dropped"):
		return
	var dir_str = dir.get_current_dir()

	if dir.file_exists("player.json"):
		var file = FileAccess.open(dir_str + "/player.json", FileAccess.READ)
		var player_data_json = file.get_as_text()
		var player_data = JSON.parse_string(player_data_json)
		player.load_dict(player_data)

	if dir.file_exists("inventories/player.json"):
		var file = FileAccess.open(dir_str + "/inventories/player.json", FileAccess.READ)
		var player_data_json = file.get_as_text()
		var player_data = JSON.parse_string(player_data_json)
		player.load_inventory_array(player_data, items)

	var external_inventories = get_tree().get_nodes_in_group("external_inventory")
	for i in external_inventories:
		var inventory_name = i.inventory_id
		if DirAccess.dir_exists_absolute(dir_str + "/inventories/external/" + inventory_name + ".json"):
			var external_inventory_data
			var external_inventory_data_json
			var file
			file = FileAccess.open(dir_str + "/inventories/external/" + inventory_name + ".json", FileAccess.READ)
			external_inventory_data_json = file.get_as_text()
			external_inventory_data = JSON.parse_string(external_inventory_data_json)
			i.load_inventory_array(external_inventory_data, items)

	var dropped_items_path = dir_str + "/inventories/dropped"
	var dropped_items_dir = DirAccess.open(dropped_items_path)
	var dropped_items = dropped_items_dir.get_files()
	for i in dropped_items:
		var node_instance = pick_upable_scene.instantiate()
		var file = FileAccess.open(dir_str + "/inventories/dropped/" + i, FileAccess.READ)
		var item_data_json = file.get_as_text()
		var item_data = JSON.parse_string(item_data_json)
		node_instance.load_slot(item_data, items, $"../player")
		$".".add_child(node_instance)
